import ReactDOM from 'react-dom/client'
import Loading from './loading'

// 同时发起多个异步请求只显示一次 loading
let count = 0
export const showLoading = () => {
  if (count === 0) {
    const loadingDiv = document.createElement('div')
    loadingDiv.setAttribute('id', 'loading')
    ReactDOM.createRoot(loadingDiv).render(<Loading />)
    console.log('显示loading')
  }
  count++
}

export const hideLoading = () => {
  if (count < 0) return
  count--
  const loadingDiv = document.getElementById('loading') as HTMLDivElement
  if (count === 0 && loadingDiv) document.body.removeChild(loadingDiv)
}
